package com.attendcollatesys.ex.handler;

import com.alibaba.excel.exception.ExcelCommonException;
import com.attendcollatesys.ex.handler.ex.DataUnFindRunTimeException;
import com.attendcollatesys.ex.handler.ex.ExcelContentCheckException;
import com.attendcollatesys.web.AppHttpCodeEnum;
import com.attendcollatesys.web.ResponseResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常捕获
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(ExcelCommonException.class)
    @ResponseBody
    public ResponseResult<?> fileConvertException(ExcelCommonException e) {
        System.out.println("全局异常捕获>>>:"+e);
        e.printStackTrace();
        return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, "文件格式异常");
    }

    @ExceptionHandler(ExcelContentCheckException.class)
    @ResponseBody
    public ResponseResult<?> ExcelContentCheck(ExcelContentCheckException e) {
        System.out.println("全局异常捕获>>>:" + e);
        e.printStackTrace();
        return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR,e.getMessage());
    }

    @ExceptionHandler(DataUnFindRunTimeException.class)
    @ResponseBody
    public ResponseResult<?> DataUnfind(DataUnFindRunTimeException e) {
        System.out.println("全局异常捕获>>>:" + e);
        e.printStackTrace();
        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_UNFIND,e.getMessage());
    }




    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public ResponseResult<?> runTimeExceptionHandler(RuntimeException e) {
        System.out.println("全局异常捕获>>>:" + e);
        e.printStackTrace();
        return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR, e.getMessage());
    }


    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseResult<?> exceptionHandler(Exception e){
        System.out.println("全局异常捕获>>>:"+e);
        e.printStackTrace();
        return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR,e.getCause().getMessage());
    }




}
